/**
 * Copyright 2014,Peak Tai 台俊峰(taijunfeng_it@sina.com).
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * 		http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.cenluan.blog;

import com.jfinal.core.Controller;
import com.jfinal.validate.Validator;

public class BlogValidator extends Validator {

	@Override
	protected void validate(Controller c) {
		validateString("blog.title", 1, 32, "msg", "文章标题必须在32字以内");
		validateRequired("blog.content", "msg", "内容不能为空");
		String type = c.getPara("blog.type");
		if (!Blog.TYPE_ORIGINAL.equals(type) && !Blog.TYPE_COPY.equals(type)) {
			addError("msg", "未知的类型");
		}
		if (Blog.TYPE_COPY.equals(type)) {
			validateUrl("blog.referer", "msg", "请输入正确的转载出处地址");
			validateString("blog.referer", 12, 1000, "msg", "引用地址长度请保持在12-1000");
		}
		if (!"true".equals(c.getPara("blog.privacy"))
				&& !"false".equals(c.getPara("blog.privacy"))) {
			addError("msg", "博客隐私设置不正确");
		}
	}

	@Override
	protected void handleError(Controller c) {
		c.renderJson();
	}

}
